<!DOCTYPE html>
<html>
<head>
    <meta charset='UTF-8'>
    <title>set</title>
    <script src="../js/vue.js"></script>
</head>
<body>
    <div id="root">
        <h1>学生信息</h1>

        <button @click="student.age++">年龄+1</button>
        <button @click.once="addSex">添加性别属性，默认值男</button>
        <button @click="addF">在列表首位添加一个朋友</button>
        <button @click="updateF">修改第一个朋友名字为张三</button>
        <button @click="addH">添加一个爱好</button>
        <button @click="updateH">修改第一个爱好为：看书</button>
        <button @click="remove">过滤掉爱好</button>



        <h2>学生名称：{{student.name}}</h2>
        <h2 v-if="student.sex">学生性别：{{student.sex}}</h2>
        <h2>学生年龄：真实{{student.age.rAge}}，对外{{student.age.sAge}}</h2>
        <h2>朋友们</h2>
        <ul>
            <li v-for="(f,index) in student.friends" :key="index">
            {{f.name}}-{{f.age}}
        </li>
        </ul>
        <h2>爱好</h2>
        <ul>
            <li v-for="(h,index) in student.hobby" :key="index">
            {{h}}
        </li>
        </ul>
    </div>
</body>
<!--   JS   -->
    <script>
    const vm = new Vue({
        el:'#root',
        data:{
            student:{
                name:'Evan',
                age:{
                    rAge:21,
                    sAge:18
                },
                hobby:['吃饭','睡觉','打豆豆'],
                friends:[
                    {name:'jerry',age:15},
                    {name:'tom',age:25}
                ]
            }
        },
        methods: {
            addSex(){
                // Vue.set(this.student,'sex','男')
                this.$set(this.student,'sex','男')
            },
            addF(){
                this.student.friends.unshift({name:'qiqi',age:23})
            },
            updateF(){
                this.student.friends[0].name='张三'
            },
            addH(){
                this.student.hobby.push('学习')
            },
            updateH(){
                this.student.hobby.splice(0,1,'看书')
                this.sutdent.hobby[3]='躺平'//不奏效，Vue数组必须调用那7个函数Vue才认为你更新了数据,直接赋值的不算
            },
            remove(){
                this.student.hobby = this.student.hobby.filter((h)=>{
                    return h!=='学习'
                })

            }


        },

    })
    </script>
</html>